package demo12.gumballmonitor;

/**
 * 糖果机
 * */
public class GumballMachine {
	//将状态封装成类
	State soldOutState;
	State noQuarterState;
	State hasQuarterState;
	State soldState;
	State winnerState;
 
	State state = soldOutState;//状态
	int count = 0;//总数
	//机器的位置
 	String location;
 //构造器对象存储实例变量
	public GumballMachine(String location, int count) {
		soldOutState = new SoldOutState(this);
		noQuarterState = new NoQuarterState(this);
		hasQuarterState = new HasQuarterState(this);
		soldState = new SoldState(this);
		winnerState = new WinnerState(this);

		this.count = count;
 		if (count > 0) {
			state = noQuarterState;
		} 
		this.location = location;
	}
 
	public void insertQuarter() {
		state.insertQuarter();
	}
 
	public void ejectQuarter() {
		state.ejectQuarter();
	}
 
	public void turnCrank() {
		state.turnCrank();
		state.dispense();
	}

	void setState(State state) {
		this.state = state;
	}
 
	void releaseBall() {
		System.out.println("一个口香糖来轧出槽...");
		if (count != 0) {
			count = count - 1;
		}
	}
 //得到总数
	public int getCount() {
		return count;
	}

	public void refill(int count) {
		this.count = count;
		state = noQuarterState;
	}
//获取状态
    public State getState() {
        return state;
    }
 //获取位置
    public String getLocation() {
        return location;
    }
 
    public State getSoldOutState() {
        return soldOutState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getWinnerState() {
        return winnerState;
    }
 
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();
		result.append("\njava站口香糖模型机器\n");
		result.append("\n库存: " + count + " 糖果");
		result.append("\n");
		result.append("机器状态 是 " + state + "\n");
		result.append("机器位置在"+location+"\n");
		return result.toString();
	}
}
